package com.application.model.universidades.quevasaestudiar.manager;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringEscapeUtils;

import com.application.model.RetrivierWeb;
import com.application.model.elegircolegio.entries.ElegirColegioEntity;

public class ManagerDetalle extends RetrivierWeb{

	private ManagerQueVasEntity informacion; 
	
	public ManagerDetalle(ManagerQueVasEntity informacion) {
		this.informacion = informacion;
	}

	protected void processContentDetalle(String data,String pais,String provincia,String partido,String nombreColegio,String zonaKey) {
        Pattern     pattern = null;
        Matcher     matcher = null;
        String      regex = null;
        String      nameColegio = "";
        String      infrastructura = "";
        String      descripcion = "";
        data = unescapeHtml(data);
//        data = StringEscapeUtils.escapeSql(data);
        //regex = "<h1>(.*?)</h1>(.*?)<strong>Idiomas</strong><br />(.*?).<br />(.*?)<strong>Infraestructura</strong>(.*?)<br />(.*?)<div style=\\\"text-align:justify;\\\">(.*?)<!-- ARTICULOS COMPLEMENTARIOS -->";
        regex = "<h1>(.*?)</h1>(.*?)<div style=\\\"text-align:justify;\\\">(.*?)<!-- ARTICULOS COMPLEMENTARIOS -->";
        pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE|Pattern.MULTILINE|Pattern.DOTALL|Pattern.UNICODE_CASE|Pattern.UNIX_LINES);
        matcher = pattern.matcher(data);
        
        while (matcher.find()) {
            nameColegio = matcher.group(1);
            
//            infrastructura = getInfrastructura(matcher.group(2));
//            descripcion = getDescripcion(matcher.group(3));
//            if(nameColegio.equals("San Felipe Apóstol")){
//            	System.out.println("");
//            }
            
            logger.info("INSERT INTO detalle(infraestructura,descripcion,Institucion_id) "
                    + "VALUES("
                    + "'"+escapeSql(infrastructura)+"',"                    
                    + "'"+escapeSql(descripcion)+"',"
                    + " (SELECT id from institucion WHERE "+getWhereColegioUpdate(pais, provincia, partido, nameColegio,zonaKey)+")"
                    + ");");
//            informacion.addInfrastructuraYDescripcion(nameColegio,infrastructura,descripcion);
            
//            generarIdiomas(matcher.group(2),pais, provincia, partido, nameColegio,zonaKey);
        }
    }
	
	
	@Override
	public void processContent(String data, String parentName) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void retrieve() {
		// TODO Auto-generated method stub
		
	}

	public void retrivie(String urlDetalle,String beginPatter,String endPatter,String pais,String provincia,String partido,String nombreColegio,String zonaKey) {
		String paginaWeb = "";
		try {
			paginaWeb = getWebPageContent(urlDetalle, beginPatter, endPatter,"utf-8");
			if (!paginaWeb.isEmpty()) {
//                                if(parentName!=null){
//                                    desendencia.add(parentName);
//                                }                                
				processContentDetalle(paginaWeb,pais,provincia,partido,nombreColegio,zonaKey);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
		}
		
	}

}
